// pages/pay/pay.js
const util = require("../../utils/util")
const api = require("../../request/api")
import Toast from '../../vant/weapp/toast/toast';
import Notify from '../../vant/weapp/notify/notify';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId:null,
    marginNum:0,
    payStatus:null,
    show: false,
  },
  back(){
    this.setData({
      show: false
    });
  },
  payMargin() {
    this.setData({
      show: true
    });
  },
  check(){
    this.setData({
      show: false
    });
    if(this.data.payStatus == 1){
      this.saveMonet();
    }else{
      this.getMonet();
    }
  },
  getMonet(e){
    this.setData({ showShade: true });
    Toast.loading({
      duration: 0,
      message: '请求中...',
      forbidClick: true,
    });
    util.post(api.order.upPayStatus,{orderId:this.data.orderId,payStatus:3}).then(res => {
      this.setData({ showShade: false });
      Toast.clear();
      if(res.code == 200){
        Notify({ type: 'success', message: '取回成功' });
        setTimeout(function(){
          wx.navigateBack();
          Toast.clear();
        },2000);
      }else{
        Toast.fail(res.message);
      }
    }).catch(() => {
      this.setData({ showShade: false });
      Toast.clear();
    })
  },
  saveMonet(e){
    this.setData({ showShade: true });
    Toast.loading({
      duration: 0,
      message: '支付中...',
      forbidClick: true,
    });
    util.post(api.order.upPayStatus,{orderId:this.data.orderId,payStatus:2}).then(res => {
      this.setData({ showShade: false });
      Toast.clear();
      if(res.code == 200){
        Notify({ type: 'success', message: '支付成功' });
        setTimeout(function(){
          wx.navigateBack();
          Toast.clear();
        },2000);
      }else{
        Toast.fail(res.message);
      }
      this.getMyOrder();
    }).catch(() => {
      this.setData({ showShade: false });
      Toast.clear();
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var money;
    if (options.payStatus == 1){
      money = 50;
    }else{
      money = 0;
    }
    this.setData({
      orderId: options.orderId,
      payStatus:options.payStatus,
      marginNum: money,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})